using System;
using System.Collections.Generic;
using ACS.Runtime.Visitors;

namespace ACS.Runtime.Types
{
    public delegate int IterDategate(BaseType field);

    public class ArrayType<T> : BaseType, IArrayType where T: BaseType 
    {
        private List<T> _list;

        public int Iter(IterDategate method)
        {
            int i = 0;
            foreach (T t in _list)
            {
                i += method(t);
            }
            return i;
        }

        public ArrayType(int count)
        {
            _list = new List<T>(count);
        }
        
        public T this[int index]
        {
            get
            {
                return _list[index];
            }
        }

        public override int Accept(IVisitor visitor)
        {
            return visitor.Visit(this);
        }

        public override void Assign(BaseType from)
        {
            Assign((ArrayType<T>) from);
        }

        public void Assign(ArrayType<T> from)
        {
            int length = Math.Min(_list.Count, from._list.Count);
            for (int i = 0; i < length; i++)
            {
                _list[i].Assign(from[i]);
            }
        }
    }
}
